<template>
  <div id="h5-catch-flight-orders" class="card col-12 px-0 mt-5">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item ms-1" aria-current="page">
          <router-link to="/h5/flt/search">机票</router-link>
        </li>
        <li class="breadcrumb-item active">追位</li>
      </ol>
    </nav>
    <template v-if="showSearchForm === false">
      <div class="card-header">
        <router-link class="btn btn-success ms-auto" to="/h5/tools/catch/new"
          >新建</router-link
        >

        <button
          type="button"
          class="btn btn-primary float-end"
          @click.stop="showSearchOption()"
        >
          查找
        </button>
      </div>

      <catch-order-list
        :dataList="dataList"
        @refresh="search"
        @copyOrder="copyOrder"
        @deleteOrder="deleteOrder"
      ></catch-order-list>

      <nav id="pagination-box" class="float-end">
        <my-pagination
          name="pagination"
          :row-count="sc.rowCount"
          :page-total="sc.pageTotal"
          :page-no="sc.pageNo"
          @next-page="nextPage"
          @prev-page="prevPage"
          @direct-page="directPage"
        ></my-pagination>
      </nav>
    </template>
    <template v-else>
      <div class="card-body">
        <form class="">
          <div class="row mb-1">
            <input
              type="textfield"
              class="form-control form-control-sm"
              size="6"
              placeholder="编码"
              v-model.trim="pnrNo"
            />
          </div>
          <div class="row mb-1">
            <my-date-picker
              id="beginDate"
              name="sc.beginDate"
              v-model="beginDate"
              placeholder="起飞日期"
            ></my-date-picker>
          </div>
          <div class="row mb-1">
            <select
              class="form-select"
              v-model.number="status"
              name="sc.status"
            >
              <option value="-1">所有状态</option>
              <option value="0">待处理</option>
              <option value="1">处理中</option>
              <option value="2">已完成</option>
              <option value="4">已取消</option>
            </select>
          </div>
          <div class="row mb-1">
            <select
              class="form-select form-control-sm"
              v-model.number="sc.pageSize"
            >
              <option value="5">5</option>
              <option value="10">10</option>
              <option value="20">20</option>
              <option value="50">50</option>
            </select>
          </div>
          <div class="row mb-1">
            <button
              type="button"
              class="btn btn-primary mb-2"
              @click.stop="search()"
            >
              确定
            </button>
            <button
              type="button"
              class="btn btn-secondary"
              @click.stop="reset()"
            >
              重置
            </button>
          </div>
        </form>
      </div>
    </template>
  </div>
</template>

<script>
import {
  searchCatchOrders,
  deleteOrder,
  checkExpireDate,
} from "@/api/catch-order.js";
import MyPagination from "@/components/my-pagination_h5.vue";
import CatchOrderList from "@/components/list/catch-order-list_h5.vue";
import MyDatePicker from "@/components/my-datepicker.vue";

export default {
  name: "CatchOrders",
  components: {
    MyPagination,
    CatchOrderList,
    MyDatePicker
  },
  data() {
    return {
      dataList: [],

      showSearchForm: false,
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },

      tips: "",

      pnrNo: "",
      status: -1,
      intlTicket: -1,
      orderNo: "",
      beginDate: "",

      stat: null,
    };
  },
  mounted: function () {
    // this.search()
  },
  activated: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", {
        loading: false,
      });
    },
    checkExpireDate: function () {
      checkExpireDate((v) => {
        if (v.status === "OK") {
          this.tips = v.desc;
        } else {
          this.tips = v.errmsg;
        }
      });
    },
    showSearchOption: function () {
      this.showSearchForm = true;
    },
    search: function () {
      this.showSearchForm = false;
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.departureDate": this.beginDate,
        "sc.endDate": this.endDate,
        "sc.name": this.name,
        "sc.idno": this.idno,
        "sc.status": this.status,
        "sc.customerId": this.customerId,
        "sc.ticketNo": this.ticketNo,
        "sc.intlTicket": this.intlTicket,
        "sc.pnrNo": this.pnrNo,
        "sc.workStatus": this.workStatus,
        "sc.onlyMine": this.onlyMine,
        "sc.op1": this.op1,
        "sc.orderNo": this.orderNo,
        "sc.payStatus": this.payStatus,
      };
      this.showLoading("查询中");
      searchCatchOrders(
        params,
        (jsonResult) => {
          this.dataList = jsonResult.dataList;
          this.sc = jsonResult.page;
        },
        () => {
          this.hideLoading();
        }
      );
    },
    reset: function () {
      this.status = -1;
      this.sc.pageNo = 1;
      this.beginDate = "";
      this.pnrNo = "";
    },
    copyOrder: function (id) {
      this.$router.push("/h5/tools/catch/new?copyId=" + id);
    },
    deleteOrder: function (id) {
      deleteOrder(id, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("操作成功");
          this.search();
        } else {
          this.showErrMsg("操作失败", "danger");
        }
      });
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
